package com.cqfae.deploycentral.app.web.controller;

/**
 * Created by YANGHENG940 on 2017/7/12.
 */

import com.cqfae.deploycentral.app.utils.GsonUtils;
import com.cqfae.deploycentral.app.base.BaseConstants;
import com.cqfae.deploycentral.app.dao.entity.ReportInfoTag;
import com.cqfae.deploycentral.app.services.CommonControllerService;
import com.cqfae.deploycentral.app.services.DatePlateService;
import com.cqfae.deploycentral.app.services.ReportInfoService;
import com.cqfae.deploycentral.app.services.WindowPlansService;
import com.cqfae.deploycentral.app.utils.NullUtil;
import com.cqfae.deploycentral.app.web.request.ReportInfoRequest;
import com.cqfae.deploycentral.app.web.request.WindowPlansRequest;
import com.cqfae.deploycentral.app.web.response.WebResponse;
import com.cqfae.deploycentral.app.web.vo.WindowPlansTagVo;
import com.cqfae.deploycentral.app.web.vo.WindowPlansVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>窗口计划controller</p>
 *
 * @author yangheng@cmhk.com
 * @since 1.0.0-snapshot
 */
@RestController
@RequestMapping("/deploy-central/windows/plans")
public class WindowPlansController implements CommonControllerService<Object> {

    private static Logger logger = LoggerFactory.getLogger(WindowPlansController.class);

    @Autowired
    private WindowPlansService windowPlansService;

    @Autowired
    private ReportInfoService reportInfoService;

    @Autowired
    private DatePlateService datePlateService;

    @RequestMapping(value = "/info")
    public WebResponse pageInfo(WindowPlansRequest request) {
        logger.info("==== pageInfo WindowPlansRequest :==== {}", GsonUtils.getInstance().toJson(request));
        return getWebResponse(windowPlansService.page(request));
    }

    //deployAction
    @RequestMapping(value = "/tags")
    public WebResponse getTags(ReportInfoRequest request) {
        logger.info("==== getTags DeployActionRequest :==== {}", GsonUtils.getInstance().toJson(request));
        List<WindowPlansTagVo> vos = null;
        if(NullUtil.isNotEmpty(request.getReportCode())){
            ReportInfoTag tag = new ReportInfoTag();
            tag.setReportCode(request.getReportCode());
            List<ReportInfoTag> tags = reportInfoService.getReportInfoTagList(tag);
            vos = windowPlansService.getWindowPlansTags(tags);
        }
        //查询出符合当大盘当前组本周的要发布的
        if(NullUtil.isNotEmpty(request.getProjectName())){
            String projectName = request.getProjectName();
            vos = datePlateService.queryProjectTagInfo(projectName);
        }
        return getWebResponse(vos);
    }

    @RequestMapping(value = "/add")
    public WebResponse addPlans(@RequestBody WindowPlansVo vo) {
        logger.info("==== addPlans WindowPlansVo :==== {}", GsonUtils.getInstance().toJson(vo));
        return getWebResponse(windowPlansService.save(vo,false));
    }

    @RequestMapping(value = "/file/upload",method = RequestMethod.POST)
    public WebResponse fileUpload(@RequestParam("file") MultipartFile file,
                                  HttpServletRequest request, HttpServletResponse response) throws IOException {
        Assert.isTrue( file.getSize() > 0 && file.getSize() < BaseConstants.FILE_LIMIT_BYTES,"文件上传大小必须在1kb到10M之间");
        return getWebResponse(windowPlansService.upload(file));
    }

    @RequestMapping(value = "/file/download")
    public void exportFile(long fileId,HttpServletResponse response)  {
        logger.info("==== exportFile fileId :==== {}", fileId);
        windowPlansService.downloadFile(fileId,response);
    }

    @RequestMapping(value = "/file/delete")
    public WebResponse deleteFile(long fileId)  {
        logger.info("==== deleteFile fileId :==== {}", fileId);
        return getWebResponse(windowPlansService.delFile(fileId));
    }

    @Override
    public WebResponse getWebResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        if (NullUtil.isNotEmpty(obj)) {
            webResponse.setData(obj);
        }
        return webResponse;
    }

}
